package com.cbg.base.utils;

import lombok.AllArgsConstructor;
import lombok.Data;

/**
 * <pre>Result Json API</pre>
 * <small> 2018-10-03 | chenbing </small>
 */
@Data
@AllArgsConstructor
public class Result<T> {
    private Integer code;
    private String msg;
    private T data;
    private Result(){}
    private Result(T data){
        this.code=CodeMsg.SUCCESS.getCode();
        this.msg=CodeMsg.SUCCESS.getMsg();
        this.data=data;
    }
    public static <T> Result<T> ok(){
        return new Result<>(null);
    }
    public static <T> Result<T> ok(T data){
        return new Result<>(data);
    }

    public static <T> Result<T> build(CodeMsg cm){
        return new Result<>(cm.getCode(),cm.getMsg(),null);
    }
    public static <T> Result<T> build(CodeMsg cm,T data){
        return new Result<>(cm.getCode(),cm.getMsg(),data);
    }

    public static <T> Result<T> fail(){
        return new Result<>(CodeMsg.Fail.getCode(),CodeMsg.Fail.getMsg(),null);
    }
    public static <T> Result<T> fail(T data){
        return new Result<>(CodeMsg.Fail.getCode(),CodeMsg.Fail.getMsg(),data);
    }
}
